﻿using System.Collections.Generic;
using System.Linq;
using DevExpress.Skins;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;

namespace CSW.Framework.UI.DevEx.Forms
{
    /// <summary>
    /// Base class for a main form with bar manager and tab control docked to client area.
    /// </summary>
    public sealed partial class DXMainForm : XtraForm
    {
        private readonly List<BarCheckItem> m_SkinBarCheckItems = new List<BarCheckItem>();

        /// <summary>
        /// Initializes a new instance of the <see cref="DXMainForm"/> class.
        /// </summary>
        public DXMainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Adds skin menu items to the specified <see cref="BarSubItem"/>.
        /// </summary>
        /// <param name="skinMenu">The skin menu.</param>
        public void AddSkinMenuItems(BarSubItem skinMenu)
        {
            foreach (SkinContainer skin in SkinManager.Default.Skins.Cast<SkinContainer>().OrderBy(p => p.SkinName))
            {
                string skinName = skin.SkinName;
                BarCheckItem skinButton = new BarCheckItem();
                skinButton.Caption = skinName;
                skinButton.Tag = skinName;
                skinButton.ItemClick += delegate { SelectSkin(skinName); };

                barManager.Items.Add(skinButton);
                skinMenu.LinksPersistInfo.Add(new LinkPersistInfo(skinButton));

                m_SkinBarCheckItems.Add(skinButton);
            }
        }

        /// <summary>
        /// Selects the skin.
        /// </summary>
        /// <param name="skinName">Name of the skin.</param>
        public void SelectSkin(string skinName)
        {
            foreach (BarCheckItem skinButton in m_SkinBarCheckItems)
            {
                skinButton.Checked = ((string) skinButton.Tag == skinName);
            }
            defaultLookAndFeel.LookAndFeel.SkinName = skinName;
        }
    }
}